import tkinter as tk
import time

class PlatformGame:
    def __init__(self, master):
        self.master = master
        self.master.title('平台跳跃')
        
        # 游戏参数
        self.width = 800
        self.height = 600
        self.gravity = 0.5
        self.jump_force = -10
        self.move_speed = 5
        
        # 玩家状态
        self.player_x = 100
        self.player_y = 300
        self.player_vy = 0  # 垂直速度
        self.on_ground = False
        self.player_size = 40
        
        # 平台数据 - [x, y, width, height]
        self.platforms = [
            [0, 500, 200, 20],      # 起始平台
            [250, 400, 200, 20],    # 中间平台
            [500, 300, 200, 20],    # 高平台
            [0, 550, 800, 50],      # 地面
        ]
        
        # 游戏状态
        self.game_over = False
        self.score = 0
        
        self.setup_ui()
        self.setup_bindings()
        self.game_loop()
        
    def setup_ui(self):
        # 创建画布
        self.canvas = tk.Canvas(
            self.master, 
            width=self.width, 
            height=self.height, 
            bg='lightblue'
        )
        self.canvas.pack()
        
        # 创建分数显示
        self.score_text = self.canvas.create_text(
            50, 30, 
            text=f"分数: {self.score}",
            font=('Arial', 16),
            fill='black'
        )
        
    def setup_bindings(self):
        # 绑定鼠标移动事件
        self.canvas.bind('<Motion>', self.handle_mouse_move)
        # 绑定鼠标点击事件（跳跃）
        self.canvas.bind('<Button-1>', self.handle_jump)
        
    def handle_mouse_move(self, event):
        # 根据鼠标位置移动角色
        if event.x > self.player_x + self.player_size:
            self.player_x += self.move_speed
        elif event.x < self.player_x:
            self.player_x -= self.move_speed
            
    def handle_jump(self, event):
        # 只有在地面上时才能跳跃
        if self.on_ground:
            self.player_vy = self.jump_force
            self.on_ground = False
            
    def check_collision(self):
        self.on_ground = False
        player_bottom = self.player_y + self.player_size
        
        for platform in self.platforms:
            px, py, pw, ph = platform
            
            # 检查是否在平台上
            if (self.player_x + self.player_size > px and 
                self.player_x < px + pw):
                
                # 从上方落到平台上
                if (player_bottom >= py and 
                    player_bottom - self.player_vy <= py + 10):
                    self.player_y = py - self.player_size
                    self.player_vy = 0
                    self.on_ground = True
                    break
                    
    def update(self):
        if not self.game_over:
            # 应用重力
            if not self.on_ground:
                self.player_vy += self.gravity
            
            # 更新玩家位置
            self.player_y += self.player_vy
            
            # 检查碰撞
            self.check_collision()
            
            # 检查是否掉出屏幕
            if self.player_y > self.height:
                self.game_over = True
                
            # 限制玩家不能超出屏幕左右边界
            if self.player_x < 0:
                self.player_x = 0
            elif self.player_x + self.player_size > self.width:
                self.player_x = self.width - self.player_size
                
            # 更新分数
            self.score += 1
            self.canvas.itemconfig(
                self.score_text, 
                text=f"分数: {self.score//10}"
            )
            
    def draw(self):
        self.canvas.delete('game')
        
        # 绘制平台
        for platform in self.platforms:
            x, y, w, h = platform
            self.canvas.create_rectangle(
                x, y, x + w, y + h,
                fill='green',
                tags='game'
            )
        
        # 绘制玩家
        self.canvas.create_rectangle(
            self.player_x, 
            self.player_y,
            self.player_x + self.player_size,
            self.player_y + self.player_size,
            fill='red',
            tags='game'
        )
        
        # 如果游戏结束，显示结束文字
        if self.game_over:
            self.canvas.create_text(
                self.width//2,
                self.height//2,
                text=f"游戏结束!\n最终分数: {self.score//10}",
                font=('Arial', 24),
                fill='red',
                tags='game'
            )
            
    def game_loop(self):
        self.update()
        self.draw()
        
        if not self.game_over:
            self.master.after(16, self.game_loop)  # 约60FPS
        
def main():
    root = tk.Tk()
    game = PlatformGame(root)
    root.mainloop()

if __name__ == "__main__":
    main() 